package com.lone.yebserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lone.yebserver.pojo.Position;
import com.lone.yebserver.pojo.RespBean;
import com.lone.yebserver.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lone
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    IPositionService positionService;

    @ApiOperation(value = "获取职位信息")
    @GetMapping("/")
    public RespBean getAllPositions() {
        return RespBean.success("success", positionService.list());
    }

    @ApiOperation(value = "获取某个职位信息")
    @GetMapping("/{id}")
    public RespBean getPosition(@PathVariable("id") Long id) {
       QueryWrapper<Position> queryWrapper =  new QueryWrapper<>();
       queryWrapper.lambda().eq(Position::getId, id);
       return RespBean.success("success", positionService.getOne(queryWrapper));
    }

    @ApiOperation(value = "添加职位信息")
    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position){
        position.setCreatedate(LocalDateTime.now());
        if (positionService.save(position)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "修改职位信息")
    @PutMapping("/")
    public RespBean updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public RespBean deletePosition(@PathVariable("id") Long id) {
        if (positionService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public RespBean deletePositions(Long[] ids) {
        if (positionService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("批量删除成功");
        }
        return RespBean.error("批量删除失败");
    }


}
